package com.hdit.guimei.web;

import com.hdit.guimei.domain.Goods_detail;
import com.hdit.guimei.service.GoodsDetailService;
import com.hdit.guimei.util.OssUtil;
import com.hdit.guimei.util.SubStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @基本功能:
 * @ClassName: GoodsDetailController
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/20 17:41
 * @Version 1.0
 */
@Controller
public class GoodsDetailController {
    @Autowired
    private GoodsDetailService goodsDetailService;

    /**
    * @Method: addGoodsDetail
    * @函数(方法)功能: 添加商品详细信息（多图片）
    * @Author: 李嘉明
    * @params:  [goods_detail, picture1, picture2, picture3, picture4, picture5]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/20 21:12
    */
    @RequestMapping("/addGoodsDetail")
    @ResponseBody
    public int addGoodsDetail(Goods_detail goods_detail, MultipartFile picture1, MultipartFile picture2, MultipartFile picture3, MultipartFile picture4, MultipartFile picture5) {
        System.out.println(goods_detail);
        if (picture1!=null&&!picture1.isEmpty()) {
            String picture1_url = new OssUtil().uploadDocument(picture1, new SubStringUtil().SubFileName(picture1.getOriginalFilename(), "."));
            goods_detail.setSmall_pic1(picture1_url);
            System.out.println(picture1_url);
        }
        if (picture2!=null&&!picture2.isEmpty()) {
            String picture2_url = new OssUtil().uploadDocument(picture2, new SubStringUtil().SubFileName(picture2.getOriginalFilename(), "."));
            goods_detail.setSmall_pic2(picture2_url);
            System.out.println(picture2_url);
        }
        if (picture3!=null&&!picture3.isEmpty()) {
            String picture3_url = new OssUtil().uploadDocument(picture3, new SubStringUtil().SubFileName(picture3.getOriginalFilename(), "."));
            goods_detail.setSmall_pic3(picture3_url);
            System.out.println(picture3_url);
        }
        if (picture4!=null&&!picture4.isEmpty()) {
            String picture4_url = new OssUtil().uploadDocument(picture4, new SubStringUtil().SubFileName(picture4.getOriginalFilename(), "."));
            goods_detail.setSmall_pic4(picture4_url);
            System.out.println(picture4_url);
        }
        if (picture5!=null&&!picture5.isEmpty()) {
            String picture5_url = new OssUtil().uploadDocument(picture5, new SubStringUtil().SubFileName(picture5.getOriginalFilename(), "."));
            goods_detail.setSmall_pic5(picture5_url);
            System.out.println(picture5_url);
        }
        System.out.println(goods_detail);
        return goodsDetailService.addGoodsDetail(goods_detail);
    }
    /**
    * @Method: updateGoodsDetail
    * @函数(方法)功能: 修改商品详情信息
    * @Author: 李嘉明
    * @params:  [goods_detail, picture1, picture2, picture3, picture4, picture5]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/21 14:28
    */
    @RequestMapping("/updateGoodsDetail")
    @ResponseBody
    public int updateGoodsDetail(Goods_detail goods_detail, MultipartFile picture1, MultipartFile picture2, MultipartFile picture3, MultipartFile picture4, MultipartFile picture5){
        System.out.println(goods_detail);
        if (picture1!=null&&!picture1.isEmpty()) {
            String picture1_url = new OssUtil().uploadDocument(picture1, new SubStringUtil().SubFileName(picture1.getOriginalFilename(), "."));
            goods_detail.setSmall_pic1(picture1_url);
            System.out.println(picture1_url);
        }else{
            goods_detail.setSmall_pic1("null");
        }
        if (picture2!=null&&!picture2.isEmpty()) {
            String picture2_url = new OssUtil().uploadDocument(picture2, new SubStringUtil().SubFileName(picture2.getOriginalFilename(), "."));
            goods_detail.setSmall_pic2(picture2_url);
            System.out.println(picture2_url);
        }else {
            goods_detail.setSmall_pic2("null");
        }
        if (picture3!=null&&!picture3.isEmpty()) {
            String picture3_url = new OssUtil().uploadDocument(picture3, new SubStringUtil().SubFileName(picture3.getOriginalFilename(), "."));
            goods_detail.setSmall_pic3(picture3_url);
            System.out.println(picture3_url);
        }else{
            goods_detail.setSmall_pic3("null");
        }
        if (picture4!=null&&!picture4.isEmpty()) {
            String picture4_url = new OssUtil().uploadDocument(picture4, new SubStringUtil().SubFileName(picture4.getOriginalFilename(), "."));
            goods_detail.setSmall_pic4(picture4_url);
            System.out.println(picture4_url);
        }else{
            goods_detail.setSmall_pic4("null");
        }
        if (picture5!=null&&!picture5.isEmpty()) {
            String picture5_url = new OssUtil().uploadDocument(picture5, new SubStringUtil().SubFileName(picture5.getOriginalFilename(), "."));
            goods_detail.setSmall_pic5(picture5_url);
            System.out.println(picture5_url);
        }else{
            goods_detail.setSmall_pic5("null");
        }
        System.out.println(goods_detail);
        return  goodsDetailService.updateGoodsDetail(goods_detail);
    }
}
